/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author rizqi
 */
public class DaftarCerita {

    Connection conn;

    public DaftarCerita(Connection connection) {
    }

    public List<Cerita> getAllCerita() throws Exception {
        List<Cerita> cerita = new ArrayList<Cerita>();
        String sql = "SELECT * FROM cerita";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Cerita prm = new Cerita();
                prm.setId(rs.getLong(1));
                //prm.setId_propinsi(Long.parseLong(rs.getString(2)));
                prm.setIsi_cerita(rs.getString(3));
                cerita.add(prm);
            }

        } catch (Exception e) {
        }
        return cerita;
    }

    //one
    public List<Cerita> selectone(int id) throws Exception {
        List<Cerita> cerita = new ArrayList<Cerita>();
        String sql = "SELECT * FROM cerita where id_cerita = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, id);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Cerita prm = new Cerita();
                prm.setId(rs.getLong(1));
                //prm.setId_propinsi(Long.parseLong(rs.getString(2)));
                prm.setIsi_cerita(rs.getString(3));
                cerita.add(prm);
            }
        } catch (Exception e) {
        }
        return cerita;
    }

    public void add(String isi_cerita) throws Exception {
        String sql = "INSERT INTO cerita VALUES (NULL, NULL, )";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, isi_cerita);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
        }
    }

    public void update(Long id_cerita, String id_propinsi, String isi_cerita) throws Exception {
        String sql = "UPDATE cerita SET isi_cerita=? WHERE id_cerita=?";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, isi_cerita);
            st.setLong(7, id_cerita);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
        }
    }

    public void delete(Long id_cerita) {

        String sql = "DELETE FROM cerita WHERE id_cerita = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setLong(1, id_cerita);

            st.executeUpdate();
            st.close();
        } catch (Exception e) {
        }
    }
}